﻿/*
-----------------------------------------------------------------------------
This source file is part of Cell Cloud.

Copyright (c) 2009-2012 Cell Cloud Team - cellcloudproject@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------
*/

#ifndef CellCloudGraph_h
#define CellCloudGraph_h

#include "CellPrerequisites.h"
#include "CellCellet.h"

namespace cell
{
/** \addtogroup Core
 *  @{
 */
/** \addtogroup General
 *  @{
 */

class CelletIndexTable;

typedef std::list<Endpoint*> EndpointList;

/** 云图，描述了当前内核所在云内的位置和关联信息。
云图中的每个顶点表示一个客观存在的 Cell 节点。

@author
	Jiangwei Xu
*/
class _CellExport CloudGraph : public GeneralAlloc
{
public:
	/** 默认构造函数。
	*/
	CloudGraph(void);

	/** 析构函数。
	*/
	virtual ~CloudGraph(void);

public:
	/** 插入端点。
	*/
	void insert(Endpoint* endpoint);

	/** 删除端点。
	*/
	void erase(const Endpoint* endpoint);

	/** 查找指定的端点。
	*/
	Endpoint* find(const String& tag);
	/** 查找指定的端点。
	*/
	Endpoint* find(const String& tag) const;

	/** 绑定 Cellet 服务到指定端点。
	*/
	void bindCellet(const String& identity, Endpoint* endpoint);

	/** 解除 Cellet 服务。
	*/
	void unbindCellet(const String& identity, Endpoint* endpoint);

	/** 检索具有指定 Cellet 服务的端点。
	*/
	void searchEndpoint(EndpointList& outList, const String& identity);

private:
	typedef stx::btree_map<String, Endpoint*> Endpoints;

	CELL_MUTEX(mEndpointMutex);
	Endpoints mEndpoints;
	CelletIndexTable* mCelletIndexTable;
};



/**
*/
class _CellExport CelletIndexTable : public GeneralAlloc
{
public:
	/**
	*/
	CelletIndexTable(void);

	/**
	*/
	virtual ~CelletIndexTable(void);

public:
	/**
	*/
	void insert(const String& identity, Endpoint* endpoint);

	/**
	*/
	void erase(const String& identity, Endpoint* endpoint);

	/**
	*/
	void search(EndpointList& outList, const String& identity);

private:
	typedef stx::btree_map<String, EndpointList*> EndpointLists;

	CELL_MUTEX(mListMutex);
	EndpointLists mEndpointLists;
};

/** @} */
/** @} */
} // end namespace cell

#endif // CellCloudGraph_h
